package com.ttg.web.service.Impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.handler.inter.IWriter;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.entities.BasePage;
import com.ttg.common.enums.*;
import com.ttg.common.exception.BaseException;
import com.ttg.common.utils.DateTimeUtil;
import com.ttg.model.dto.*;

import com.ttg.model.dto.open.TagStatisticsDto;
import com.ttg.model.dto.open.VipTagDto;
import com.ttg.model.mapper.TagHisMapper;
import com.ttg.model.pojo.*;
import com.ttg.model.vo.*;
import com.ttg.model.vo.open.TagStatisticsVo;
import com.ttg.model.vo.open.VipTagVo;
import com.ttg.web.dao.*;
import com.ttg.web.service.TagHisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class TagHisServiceImpl extends ServiceImpl<TagHisMapper, TagHis> implements TagHisService {

    @Autowired
    private TagHisDao tagHisDao;

    @Override
    public BasePage<TagHisVo> tagHisPage(TagHisDTO reqDto) {
        if (Objects.isNull(reqDto)){
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }
        BasePage<TagHisVo> tagVoBasePage = tagHisDao.tagHisPage(reqDto);
        return tagVoBasePage;
    }
    @Override
    public BasePage<TagStatisticsVo> getTagStatistics(TagStatisticsDto reqDto) {
        if (Objects.isNull(reqDto)){
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }
        BasePage<TagStatisticsVo> tagVoBasePage = tagHisDao.getTagStatistics(reqDto);
        return tagVoBasePage;
    }

    @Override
    public BasePage<VipTagVo> getVipTag(VipTagDto reqDto) {
        if (Objects.isNull(reqDto)){
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }
        BasePage<VipTagVo> tagVoBasePage = tagHisDao.getVipTag(reqDto);
        return tagVoBasePage;
    }


    @Override
    public void export(TagHisDTO reqDto, HttpServletResponse response) {
        String fileName = "tagExport";
        String fileType = ".xlsx";
        int extCount = 0;
        reqDto.setPage(1);
        reqDto.setPageSize(1000);
        BasePage<TagHisVo> basePage = tagHisDao.tagHisPage(reqDto);
        List<TagHisVo> list = basePage.getList();
        List<TagHisVo> extList = new ArrayList<>();
        ExportParams params = new ExportParams(fileName + "导出时间：" + DateTimeUtil.formatYYYY_MM_DD_HH_MM_SS(LocalDateTime.now()) + "", "标签列表");
        params.setCreateHeadRows(true);
        IWriter<Workbook> workbookIWriter = ExcelExportUtil.exportBigExcel(params, TagHisVo.class);
        long total = basePage.getTotal();
        for (int i = 1; i <= 100; i++) {
            if (i != 1) {
                reqDto.setPage(i);
                basePage = tagHisDao.tagHisPage(reqDto);
            }
            list = basePage.getList();
            extCount += CollectionUtils.isEmpty(list) ? 0 : list.size();
            for (TagHisVo tempVo : list) {
                extList.add(tempVo);
            }
            list.clear();
            if (extCount >= total) {
                break;
            }
        }
        workbookIWriter.write(extList);
        Workbook workbook = workbookIWriter.get();
        if (workbook != null) {
            try (ServletOutputStream outputStream = response.getOutputStream()) {
                response.setCharacterEncoding("UTF-8");
                response.setHeader("content-Type", "application/vnd.ms-excel");
                response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName + fileType, "UTF-8"));
                response.setHeader("Access-Control-Expose-Headers", "filename");
                response.setHeader("filename", java.net.URLEncoder.encode(fileName + fileType, "UTF-8"));
                workbook.write(outputStream);
            } catch (IOException e) {
                log.error("export tag  统计导出异常", e);
                throw new BaseException(ErrorCodeEnums.EXPORT_IS_ERROR);
            }
        }
    }

}
